# -*- coding: utf-8 -*-


from monitor import *




def cal_profit_info(benjin , price_info , usd2cny ,buyedcoinnum):
    stamp_usd2cny = Decimal(7.15)
    usd2cny_global = Decimal(6.80401709)
    profit = {}

    for ir in bittrex_exchange_coin_type:
        for it in bittrex_buyin_coin_type:
            if(buyedcoinnum == Decimal(0)):
                profit["bter_num_" + it] = bter_buy_inqueue(it,benjin,price_info)
            elif (buyedcoinnum > Decimal(0)):
                profit["bter_num_" + it] = buyedcoinnum
            profit["bittrex_num_%s_%s" % (it,ir)] = bittrex_exchange_inqueue(it,ir,profit["bter_num_" + it],price_info)
            profit["bter_to_bittrex_benefit_" + it] = bter_sellprice_inqueue(ir,profit["bittrex_num_%s_%s" % (it,ir)],price_info) - benjin

        for it in poloniex_buyin_coin_type:
            if (buyedcoinnum == Decimal(0)):
                profit["bter_num_" + it] = bter_buy_inqueue(it, benjin, price_info)
            elif (buyedcoinnum > Decimal(0)):
                profit["bter_num_" + it] = buyedcoinnum
            profit["poloniex_num_%s_%s" % (it, ir)] = poloniex_exchange_inqueue(it, ir, profit["bter_num_" + it],
                                                                                price_info)
            profit["bter_to_poloniex_benefit_" + it] = bter_sellprice_inqueue(ir,
                                                                              profit["poloniex_num_%s_%s" % (it, ir)],
                                                                              price_info) - benjin



            # profit["bter_num_" + ir] = bter_buy_inqueue(ir, benjin, price_info)
            # profit["bittrex_reverse_num_%s_%s" % (it, ir)] = bittrex_exchange_sell_inqueue(it, ir, profit[
            #     "bter_num_" + ir], price_info)
            # profit["bter_to_bittrex_reverse_benefit_" + it] = bter_sellprice_inqueue(it, profit["bittrex_reverse_num_%s_%s" % (it, ir)], price_info) - benjin
    return profit



def bter_buy_inqueue(cointype,benjin,price_info):
    btce_obj = price_info["bter_" + cointype]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin)
    coinnum = Decimal(0.0)
    for it in btce_obj["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        coinnum += Decimal(it[1])
        if (remind <= 0):
            duoyucoin = (Decimal(0) - remind) / Decimal(it[0])
            coinnum = coinnum - duoyucoin
            return coinnum

def bittrex_exchange_inqueue(srctype,destype,coinnum,price_info):
    btce_obj = price_info["bittrex_%s_%s" % (destype,srctype)]
    if btce_obj is None: return Decimal(-1)
    remind = coinnum
    result = Decimal(0.0)
    for it in btce_obj["result"]["buy"]:
        remind = remind - Decimal(it["Quantity"])
        result += Decimal(it["Quantity"]) * Decimal(it["Rate"])
        if (remind <= 0):
            duoyu = (Decimal(0) - remind) * Decimal(it["Rate"])
            return result - duoyu

def poloniex_exchange_inqueue(srctype,destype,coinnum,price_info):
    btce_obj = price_info["poloniex_%s_%s" % (destype,srctype)]
    if btce_obj is None: return Decimal(-1)
    remind = coinnum
    result = Decimal(0.0)
    for it in btce_obj["bids"]:
        remind = remind - Decimal(it[1])
        result += Decimal(it[1]) * Decimal(it[0])
        if (remind <= 0):
            duoyu = (Decimal(0) - remind) * Decimal(it[0])
            return result - duoyu


def bittrex_exchange_sell_inqueue(srctype,destype,coinnum,price_info):
    btce_obj = price_info["bittrex_%s_%s" % (destype,srctype)]
    if btce_obj is None: return Decimal(-1)
    remind = coinnum
    result = Decimal(0.0)
    for it in btce_obj["result"]["sell"]:
        remind = remind - Decimal(it["Quantity"]) * Decimal(it["Rate"])
        result += Decimal(it["Quantity"])
        if (remind <= 0):
            duoyu = (Decimal(0) - remind) / Decimal(it["Rate"])
            return result - duoyu

def bter_sellprice_inqueue(cointype, coinnum, price_info):
    btce_obj = price_info["bter_" + cointype]
    if btce_obj is None: return Decimal(-1)
    remind = coinnum
    money = Decimal(0.0)
    for it in btce_obj["bids"]:
        remind = remind - Decimal(it[1])
        money += Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            duoyu = (Decimal(0) - remind) * Decimal(it[0])
            return money - duoyu

##########


def okcoin_btc2cny_buyprice_inqueue(benjin,price_info):
    btce_obj = price_info[okcoin_btc2cny_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin)
    for it in btce_obj["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def btce_btc2usd_buyprice_inqueue(benjin,price_info,usd2cny):
    btce_obj = price_info[btce_btc2usd_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin) / usd2cny
    for it in btce_obj["btc_usd"]["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def btce_eth2usd_buyprice_inqueue(benjin,price_info,usd2cny):
    btce_obj = price_info[btce_eth2usd_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin) / usd2cny
    for it in btce_obj["eth_usd"]["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def stamp_btc2usd_buyprice_inqueue(benjin,price_info,usd2cny):
    btce_obj = price_info[stamp_btc2usd_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin) / usd2cny
    for it in btce_obj["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def stamp_xrp2usd_buyprice_inqueue(benjin,price_info,usd2cny):
    btce_obj = price_info[stamp_xrp2usd_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin) / usd2cny
    for it in btce_obj["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def stamp_btc2usd_sell_inqueue(btcnum,price_info):
    btce_obj = price_info[stamp_btc2usd_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = btcnum
    for it in btce_obj["bids"]:
        remind = remind - Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def biter_eth2cny_buyprice_inqueue(benjin,price_info):
    btce_obj = price_info[biter_eth2cny_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin)
    for it in btce_obj["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])



def biter_eth2cny_sellprice_inqueue(ethnum,price_info):
    btce_obj = price_info[biter_eth2cny_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = ethnum
    for it in btce_obj["bids"]:
        remind = remind - Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def biter_ltc2cny_sellprice_inqueue(ltcnum,price_info):
    btce_obj = price_info[biter_ltc2cny_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = ltcnum
    for it in btce_obj["bids"]:
        remind = remind - Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def biter_ltc2cny_buyprice_inqueue(benjin,price_info):
    btce_obj = price_info[biter_ltc2cny_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin)
    for it in btce_obj["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def coinone_btc2usd_sellprice_inqueue(coinnum,price_info):
    btce_obj = price_info[coinone_btc2kwr_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = coinnum
    for it in btce_obj["bid"]:
        remind = remind - Decimal(it["qty"])
        if (remind <= 0):
            return Decimal(it["price"])


def coinone_eth2usd_sellprice_inqueue(coinnum,price_info):
    btce_obj = price_info[coinone_eth2kwr_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = coinnum
    for it in btce_obj["bid"]:
        remind = remind - Decimal(it["qty"])
        if (remind <= 0):
            return Decimal(it["price"])

def coinone_xrp2kwr_sellprice_inqueue(coinnum,price_info):
    btce_obj = price_info[coinone_xrp2usd_bid_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = coinnum
    for it in btce_obj["bid"]:
        remind = remind - Decimal(it["qty"])
        if (remind <= 0):
            return Decimal(it["price"])

def btce_eth2usd_sell_inqueue(ethnum,price_info):
    btce_obj = price_info[btce_eth2usd_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = ethnum
    for it in btce_obj["eth_usd"]["bids"]:
        remind = remind - Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def btce_ltc2usd_sell_inqueue(ltcnum,price_info):
    btce_obj = price_info[btce_ltc2usd_queue]
    if btce_obj is None: return Decimal(-1)
    remind = ltcnum
    for it in btce_obj["ltc_usd"]["bids"]:
        remind = remind - Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def btce_ltc2btc_sell_inqueue(ltcnum,price_info):
    btce_obj = price_info[btce_ltc2btc_queue]
    if btce_obj is None: return Decimal(-1)
    remind = ltcnum
    for it in btce_obj["ltc_btc"]["bids"]:
        remind = remind - Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def btce_ltc2usd_buy_inqueue(benjin,price_info,usd2cny):
    btce_obj = price_info[btce_ltc2usd_queue]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin) / usd2cny
    for it in btce_obj["ltc_usd"]["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def btce_eth2ltc_buy_inqueue(ltcnum,price_info):
    btce_obj = price_info[btce_eth2ltc_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(ltcnum)
    for it in btce_obj["eth_ltc"]["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def okcoin_ltc2cny_buy_inqueue(benjin,price_info):
    btce_obj = price_info[okcoin_ltc2cny_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin)
    for it in btce_obj["asks"]:
        remind = remind - Decimal(it[0]) * Decimal(it[1])
        if (remind <= 0):
            return Decimal(it[0])

def coinone_eth_buy_inqueue(benjin,price_info):
    btce_obj = price_info[coinone_eth2kwr_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin)
    for it in btce_obj["ask"]:
        remind = remind - Decimal(it["price"]) * Decimal(it["qty"])
        if (remind <= 0):
            return Decimal(it["price"])

def coinone_btc_buy_inqueue(benjin,price_info):
    btce_obj = price_info[coinone_btc2kwr_depthq]
    if btce_obj is None: return Decimal(-1)
    remind = Decimal(benjin)
    for it in btce_obj["ask"]:
        remind = remind - Decimal(it["price"]) * Decimal(it["qty"])
        if (remind <= 0):
            return Decimal(it["price"])

